package com.zhuiyun.project.api.collectVideo.service.impl;

import com.zhuiyun.project.api.collectVideo.entity.Favorites;
import com.zhuiyun.project.api.collectVideo.mapper.FavoritesMapper;
import com.zhuiyun.project.api.collectVideo.model.FavoritesModel;
import com.zhuiyun.project.api.collectVideo.service.FavoritesService;
import com.zhuiyun.project.api.frontuser.entity.FrontUser;
import com.zhuiyun.project.api.headportrait.entity.SysHeadPortrait;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName FavoritesServiceImpl
 * @Description TODO 收藏夹服务实现类
 * @Author XC8
 * @Date 2024/3/4 9:34
 **/
@Service
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    private FavoritesMapper favoritesMapper;

    /**
     * @Author XC8
     * @Description id查询收藏夹
     * @Date 2024/3/4 9:37
     * @Return com.zhuiyun.project.common.response.CommonResult

     */
    @Override
    public CommonResult getFavoritesById(Integer userId) throws CommonException {
        return CommonResult.ok(favoritesMapper.getFavoritesById(userId));
    }

    /**
    * @Author XC8
    * @Description 新增收藏夹
    * @Date 2024/3/4 14:49
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @Override
    public CommonResult addFavorite(FavoritesModel favoritesModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(favoritesModel)) {
            Favorites favorites = new Favorites();
            BeanUtils.copy(favoritesModel, favorites);
            int i = favoritesMapper.addFavorite(favorites);
            if (i > 0) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author XC8
    * @Description 根据id删除收藏夹
    * @Date 2024/3/4 19:49
    * @Return com.zhuiyun.project.common.response.CommonResult

    */
    @Override
    public CommonResult favoritesDelete(Integer fId) {
        if(fId!=0){
            int i=favoritesMapper.favoritesDelete(fId);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.error(EmErrorCode.DELETE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }

}
